﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace cpe.cpein.SERVER
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class UsersService : IUsersService
    {
        #region IUsersService Membres

        /// <summary>
        /// 
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<RelationType> GetAllRelationType(bool searchInDeleted, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.RelationType> types = new List<DATA.RelationType>(BUSINESS.BURelationType.GetAllRelationType(searchInDeleted));
            List<RelationType> wsTypes = new List<RelationType>();
            foreach (DATA.RelationType tmp in types)
            {
                wsTypes.Add(Tools.ObjectConverter.RelationTypeConverter(tmp));
            }

            return wsTypes;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public RelationType GetRelationType(User user, User current)
        {
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            DATA.RelationType type = BUSINESS.BURelationType.GetRelationType(Tools.ObjectConverter.UserConverter(user), Tools.ObjectConverter.UserConverter(current));
            RelationType wsType = Tools.ObjectConverter.RelationTypeConverter(type);

            return wsType;
        }

        public bool ChangeRelationType(User user1, User user2, int type, User current)
        {
            if (user1 == null || user2 == null || type <= 0 || current == null)
                return false;

            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;

            bool ret = BUSINESS.BURelationType.ChangeRelationType(Tools.ObjectConverter.UserConverter(user1), Tools.ObjectConverter.UserConverter(user2), type);
            return ret;
        }

        /// <summary>
        /// Get All the Users of the database.
        /// </summary>
        /// <param name="searchInDeleted"> Search into the deleted users too</param>
        /// /// <param name="user">Current connected user</param>
        /// <returns></returns>
        public List<User> GetAllUsers(bool searchInDeleted, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.User> users = new List<DATA.User>(BUSINESS.BUUsers.GetAllUsers(searchInDeleted));
            List<User> wsUsers = new List<User>();
            foreach (DATA.User tmp in users)
            {
                wsUsers.Add(Tools.ObjectConverter.UserConverter(tmp));
            }

            return wsUsers;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<User> GetRelations(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.User> users = new List<DATA.User>(BUSINESS.BUUsers.GetRelations(Tools.ObjectConverter.UserConverter(user)));
            List<User> wsUsers = new List<User>();
            foreach (DATA.User tmp in users)
            {
                wsUsers.Add(Tools.ObjectConverter.UserConverter(tmp));
            }

            return wsUsers;
        }

        /// <summary>
        /// Get Potential contact for a user
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<User> GetPotentialRelations(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.User> users = new List<DATA.User>(BUSINESS.BUUsers.GetPotentialRelations(Tools.ObjectConverter.UserConverter(user)));
            List<User> wsUsers = new List<User>();
            foreach (DATA.User tmp in users)
            {
                wsUsers.Add(Tools.ObjectConverter.UserConverter(tmp));
            }

            return wsUsers;
        }

        /// <summary>
        /// Get the User by id.
        /// </summary>
        /// <param name="id"> Id of the user to get </param>
        /// <param name="user">Current connected user</param>
        /// <returns></returns>
        public User GetUserByID(int id, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            if (id > 0)
            {
                DATA.User user = BUSINESS.BUUsers.GetUserByID(id);
                if(user != null)
                    return Tools.ObjectConverter.UserConverter(user);
            }

            return null;
        }

        /// <summary>
        /// Get the Group by id.
        /// </summary>
        /// <param name="id"> Id of the Group to get </param>
        /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public Group GetGroupByID(int id, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            if (id > 0)
            {
                DATA.Group Group = BUSINESS.BUGroup.GetGroupByID(id);
                if (Group != null)
                    return Tools.ObjectConverter.GroupConverter(Group);
            }

            return null;
        }

        /// <summary>
        /// Add a User into the database.
        /// </summary>
        /// <param name="user">User to add</param>
        /// <param name="user">Current connected user</param>
        /// <returns></returns>
        public User AddUser(User user, User current)
        {
            DATA.User newUser = BUSINESS.BUUsers.AddUser(Tools.ObjectConverter.UserConverter(user));
            return Tools.ObjectConverter.UserConverter(newUser);
        }

        /// <summary>
        /// Remove a specific User of the database.
        /// </summary>
        /// <param name="user"> User to remove </param>
        /// <param name="user">Current connected user</param>
        /// <returns></returns>
        public bool RemoveUser(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;

            if (user != null)
            {
                return BUSINESS.BUUsers.RemoveUser(Tools.ObjectConverter.UserConverter(user));
            }

            return false;
        }

        /// <summary>
        /// Update the User into the database.
        /// </summary>
        /// <param name="user">User to update</param>
        /// <param name="user">Current connected user</param>
        /// <returns></returns>
        public User UpdateUser(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            //SERVER.User u = user as SERVER.User;

            DATA.User newUser = BUSINESS.BUUsers.UpdateUser(Tools.ObjectConverter.UserConverter(user));

            return Tools.ObjectConverter.UserConverter(newUser);
        }

        /// <summary>
        /// Add the Group into the DB
        /// </summary>
        /// <param name="group"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public Group AddGroup(Group group, User current)
        {
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            BUSINESS.BUGroupType.GetGroupTypeByID(group.IDType);
            DATA.Group newGroup = BUSINESS.BUGroup.AddGroup(Tools.ObjectConverter.GroupConverter(group));
            return Tools.ObjectConverter.GroupConverter(newGroup);
        }

        /// <summary>
        /// Update the Group into the database.
        /// </summary>
        /// <param name="group">Group to update</param>
        /// <param name="group">Current connected Group</param>
        /// <returns></returns>
        public Group UpdateGroup(Group group, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            return Tools.ObjectConverter.GroupConverter(BUSINESS.BUGroup.UpdateGroup(Tools.ObjectConverter.GroupConverter(group)));
        }

        /// <summary>
        /// Function used to disconnect the specified user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool DisconnectUser(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;
            else
            {
                return BUSINESS.BUUsers.DiconnectUser(Tools.ObjectConverter.UserConverter(user));
            }
        }

        /// <summary>
        /// Function used to get all the user news feeds.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<Message> GetNewsFeed(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
                return Tools.ObjectConverter.MessageConverter(BUSINESS.BUUsers.GetNewsFeeds(Tools.ObjectConverter.UserConverter(user))); 
        }

        /// <summary>
        /// Function used to get all the user news feeds.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public Message AddNews(Message msg, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
                return Tools.ObjectConverter.MessageConverter(BUSINESS.BUMessage.AddMessage(Tools.ObjectConverter.MessageConverter(msg)));
        }

        /// <summary>
        /// Function used to get the groups of the user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<Group> GetGroups(User user, User current)
        {
            // Check if the user is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
                return Tools.ObjectConverter.GroupConverter(BUSINESS.BUUsers.GetGroups(Tools.ObjectConverter.UserConverter(user))); 
        }

        /// <summary>
        /// Get All the Group of the database.
        /// </summary>
        /// <param name="searchInDeleted"> Search into the deleted Group too</param>
        /// /// <param name="Group">Current connected Group</param>
        /// <returns></returns>
        public List<Group> GetAllGroups(bool searchInDeleted, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.Group> Group = new List<DATA.Group>(BUSINESS.BUGroup.GetAllGroup(searchInDeleted));
            List<Group> wsGroup = new List<Group>();
            foreach (DATA.Group tmp in Group)
                wsGroup.Add(Tools.ObjectConverter.GroupConverter(tmp));

            return wsGroup;
        }

        /// <summary>
        /// Function used to get all the GroupTypes.
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<GroupType> GetAllGroupTypes(User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
            {
                return Tools.ObjectConverter.GroupTypeConverter(BUSINESS.BUGroupType.GetAllGroupType(false));
            }
        }

        /// <summary>
        /// Function used to get all the countries.
        /// </summary>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<Country> GetAllCountries(User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
            {
                return Tools.ObjectConverter.CountryConverter(BUSINESS.BUCountry.GetAllCountry(false));
            }
        }

        /// <summary>
        /// Function used to get aal the members of a group.
        /// </summary>
        /// <param name="group"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<User> GetGroupMembers(Group group, User current)
        {
            // Check if the Group is connected 
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;
            else
            {
                return Tools.ObjectConverter.UserConverter(BUSINESS.BUGroup.GetGroupMembers(Tools.ObjectConverter.GroupConverter(group)));
            }
        }

        /// <summary>
        /// Function used to connect a user.
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User ConnectUser(string login, string password)
        {
            return Tools.ObjectConverter.UserConverter(BUSINESS.BUUsers.ConnectUser(login, password));
        }

        /// <summary>
        /// Function used to register a user.
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User RegisterUser(string login, string password)
        {
            User newU = new User();
            newU.Login = login;
            newU.Password = password;
            newU.Name = login;
            newU.Lastname = login;
            newU.IsDeleted = false;
            newU.IDAddress = 0;
            newU.Email = "";
            return Tools.ObjectConverter.UserConverter(BUSINESS.BUUsers.AddUser(Tools.ObjectConverter.UserConverter(newU)));
        }

        /// <summary>
        /// Function used to clone a user.
        /// </summary>
        /// <returns></returns>
        public User CloneUser(User user)
        {
            DATA.User temp = Tools.ObjectConverter.UserConverter(user).Clone() as DATA.User;
            return Tools.ObjectConverter.UserConverter(temp);
        }

        /// <summary>
        /// Function used to get skills from a user.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public List<Skill> GetSkills(User user, User current)
        {
            if (!Tools.UserSecurity.IsConnectUser(current))
                return null;

            List<DATA.Skill> skills = new List<DATA.Skill>(BUSINESS.BUUsers.GetSkills(Tools.ObjectConverter.UserConverter(user)));
            List<Skill> wsSkills = new List<Skill>();
            foreach (DATA.Skill tmp in skills)
            {
                wsSkills.Add(Tools.ObjectConverter.SkillConverter(tmp));
            }

            return wsSkills;
        }

        /// <summary>
        /// Function used to connect a user to a group.
        /// </summary>
        /// <param name="group"></param>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool ConnectUserToGroup(Group group, User user, User current)
        {
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;
            else
            {
                return (BUSINESS.BUUserToGroup.UserToGroupAdd(
                            new DATA.UserToGroup()
                            {
                                grp_id = group.Id,
                                usr_id = current.Id,
                                utg_date_from = DateTime.Now,
                                utg_isdeleted = false,
                            }
                       ) != null) ? true : false;
            }
        }

        /// <summary>
        /// Function used to disconnect a user to a group.
        /// </summary>
        /// <param name="grp"></param>
        /// <param name="user"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public bool DisconnectUserToGroup(Group grp, User user, User current)
        {
            if (!Tools.UserSecurity.IsConnectUser(current))
                return false;
            else
            {
                var req = from UserToGroup in BUSINESS.BUUserToGroup.GetAllUserToGroup(false)
                          where UserToGroup.usr_id == user.Id
                             && UserToGroup.grp_id == grp.Id
                             && UserToGroup.utg_isdeleted == false
                          select UserToGroup;
                foreach (var tmp in req)
                    BUSINESS.BUUserToGroup.RemoveUserToGroup(tmp);

                return true;
            }
        }

        #endregion
    }
}
